uniform sampler2D suggestiveContourTexture, depthTexture;
uniform vec2 screenSize;
uniform float radius;
uniform bool useSmoothstep;
uniform float smoothstepStart, smoothstepEnd;
uniform vec4 color;
           
void main(void) { 
 
  float pixel;
  float minDepth= 1.0;
  float depth;
  float center= texture2D(suggestiveContourTexture, gl_FragCoord.xy/screenSize).x;
  float centerDepth= texture2D(depthTexture, gl_FragCoord.xy/screenSize).x;
  float max= 0.0;
  float darker= 0.0;
  float numPixels= 0.0; 
  float i, j;
  
  

  for(i=-radius; i<=radius; i++){ 
    for(j=-radius; j<=radius; j++){
      if(sqrt(i*i+j*j)<=radius){
	pixel = texture2D(suggestiveContourTexture, (gl_FragCoord.xy+vec2(i, j))/screenSize).x;
	if(pixel<center) darker++;
	if(pixel>max) max= pixel;
	numPixels++;
	if(centerDepth==1.0){
	  depth= texture2D(depthTexture, (gl_FragCoord.xy+vec2(i, j))/screenSize).x; 	
	  if(depth<minDepth) minDepth= depth;
        }
      }
    }	 
  } 

  float alpha= min((1.0-(darker/numPixels))*(max-center), 1.0);  
  if(alpha!=0.0){
    if(useSmoothstep) alpha= smoothstep(smoothstepStart, smoothstepEnd, alpha);
    gl_FragColor= vec4(color.rgb, alpha*color.a);
    if(centerDepth==1.0) gl_FragDepth= minDepth;
    else gl_FragDepth= gl_FragCoord.z;	
  }
  else
    discard;

//gl_FragColor= vec4(texture2D(suggestiveContourTexture, (gl_FragCoord.xy)/screenSize).xyz, 1.0);
}